package main.proyecto_pei;

import utilities.BDSQLiteHelper;
import utilities.MyService;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;



public class MainActivity extends TabActivity {
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TabHost th = getTabHost();
        TabSpec ts;
        Intent intent;
        Resources res = getResources();
        intent = new Intent().setClass(this, Calculos.class);
        ts = th.newTabSpec(getString(R.string.tabNameCalculos)).setIndicator(getString(R.string.tabNameCalculos),res.getDrawable(R.drawable.calculos)).setContent(intent);
        th.addTab(ts);
        
        intent = new Intent().setClass(this, Programacion.class);
        ts = th.newTabSpec(getString(R.string.tabNameProgramacion)).setIndicator(getString(R.string.tabNameProgramacion),res.getDrawable(R.drawable.programacion)).setContent(intent);
        th.addTab(ts);
        
        intent = new Intent().setClass(this, Configuracion.class);
        ts = th.newTabSpec(getString(R.string.tabNameConfiguracion)).setIndicator(getString(R.string.tabNameConfiguracion),res.getDrawable(R.drawable.configuracion)).setContent(intent);
        th.addTab(ts);
        
        intent = new Intent().setClass(this, AcercaDe.class);
        ts = th.newTabSpec(getString(R.string.tabNameAcercade)).setIndicator(getString(R.string.tabNameAcercade),res.getDrawable(R.drawable.acercade)).setContent(intent);
        th.addTab(ts);
//        th.getTabWidget().setHorizontalScrollBarEnabled(true);
//        th.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(100,40));
//        th.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(100,40));
//        th.getTabWidget().getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams(100,40));
//        th.getTabWidget().getChildAt(3).setLayoutParams(new LinearLayout.LayoutParams(100,40));
        
        
        BDSQLiteHelper usdbh = new BDSQLiteHelper(MainActivity.this, "DB_PEI", null, 1);
		SQLiteDatabase db = null;
		try{
			db = usdbh.getReadableDatabase();
			//Hacemos un select para verificar si ya existen datos. Si ya existen es un update, sino un insert
			Cursor c_plan = db.query("PEI_PLAN", new String[] {"ID_PLAN"}, null, null, null, null, null);
			if(c_plan.moveToFirst()){//Si ya esta creado el plan, creamos el servicio
				Intent in = new Intent(MainActivity.this,MyService.class);
				this.startService(in);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			db.close();
		}
        
    }
   
}
